from exts import db

# 用户表
class UserModel(db.Model):
    __tablename__ = "user"
    userid = db.Column(db.String(20), primary_key=True)
    username = db.Column(db.String(10))
    userpassword = db.Column(db.String(10))
    useremail = db.Column(db.String(20))
    userphone = db.Column(db.String(11))

# 图书表
class BookModel(db.Model):
    __tablename__="book"
    bookid=db.Column(db.String(15), primary_key=True)
    bookname = db.Column(db.String(50))
    booksort = db.Column(db.String(20))
    bookinfo=db.relationship('BookInfoModel',backref='book',uselist=False,cascade='all, delete-orphan')

# 图书详情表
class BookInfoModel(db.Model):
    __tablename__ = "bookinfo"
    bookid = db.Column(db.String(15),db.ForeignKey('book.bookid'), primary_key=True)
    bookauthor=db.Column(db.String(20))
    bookbinfo=db.Column(db.String(150))
    bookdinfo=db.Column(db.String(2550))
    bookprice=db.Column(db.Integer)

# 省市县三级联动表
class DistrictModel(db.Model):
    __tablename__ = "dict_district"
    id=db.Column(db.Integer,primary_key=True,autoincrement=True)
    parent=db.Column(db.String(6))
    code = db.Column(db.String(6))
    name = db.Column(db.String(16))

# 地址表
class AddressModel(db.Model):
    __tablename__="address"
    aid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid=db.Column(db.String(20))
    name=db.Column(db.String(20))
    province_name=db.Column(db.String(15))
    province_code=db.Column(db.String(6))
    city_name=db.Column(db.String(15))
    city_code=db.Column(db.String(6))
    area_name=db.Column(db.String(15))
    area_code=db.Column(db.String(6))
    zip=db.Column(db.String(6))
    address=db.Column(db.String(50))
    phone=db.Column(db.String(11))
    tel=db.Column(db.String(15))
    tag=db.Column(db.String(6))
    is_default=db.Column(db.Integer)

# 购物车商品表
class CartModel(db.Model):
    __tablename__="cart"
    cid=db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid=db.Column(db.String(20))
    pid=db.Column(db.String(20),db.ForeignKey('bookinfo.bookid'))
    price=db.Column(db.Integer)
    num=db.Column(db.Integer)

# 订单表
class OrderModel(db.Model):
    __tablename__="torder"
    oid=db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.String(20))
    recv_name=db.Column(db.String(20))
    recv_phone=db.Column(db.String(11))
    recv_province=db.Column(db.String(15))
    recv_city=db.Column(db.String(15))
    recv_area = db.Column(db.String(15))
    recv_address=db.Column(db.String(50))
    total_price=db.Column(db.Integer)
    order_time=db.Column(db.DateTime())
    # 0-待付款 1-待发货 2-待收货 3-待评价 4-已完成 -1-所有
    # 用户购买后创建订单，如果没有支付则为0，支付后为1，管理员修改待发货为待收货（1->2），用户修改待收货为待评价（2->3），评价后为已完成4
    status=db.Column(db.Integer)
    orderitem=db.relationship('OrderItemModel',backref='order',lazy=True)

# 订单商品表
class OrderItemModel(db.Model):
    __tablename__="order_item"
    id=db.Column(db.Integer, primary_key=True, autoincrement=True)
    oid=db.Column(db.Integer,db.ForeignKey('torder.oid'))
    pid=db.Column(db.String(20))
    title=db.Column(db.String(50))
    price = db.Column(db.Integer)
    num = db.Column(db.Integer)

# 收藏表
class ShouCangModel(db.Model):
    __tablename__="shoucang"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uid = db.Column(db.String(20))
    bookid = db.Column(db.String(15))
    title = db.Column(db.String(50))
    price = db.Column(db.Integer)

# 邮箱验证码表
class EmailCaptchaModel(db.Model):
    __tablename__ = "email_captcha"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(100), nullable=False)
    captcha = db.Column(db.String(100), nullable=False)

# 管理员信息表
class AdminModel(db.Model):
    __tablename__ ="admin"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(10))
    password = db.Column(db.String(20))
    phone = db.Column(db.String(11))
    email = db.Column(db.String(20))
    remark = db.Column(db.String(50))

# 图书评论表
class CommentModel(db.Model):
    __tablename__="comment"
    commentId=db.Column(db.Integer,primary_key=True, autoincrement=True)
    studentID=db.Column(db.String(20))
    bookid = db.Column(db.String(15))
    content=db.Column(db.String(300))
    commentDate=db.Column(db.DateTime())
    star=db.Column(db.Integer)